package com.bier.fishproject.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationClientOption.AMapLocationProtocol;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;

public class GetLocation {

	public AMapLocationClient locationClient = null;
	
	private Handler handler;
	
	private static GetLocation getLocation;
	
	private Context context;
	
	public static GetLocation getIntesen(){
		if(getLocation==null){
			getLocation=new GetLocation();
		}
		return getLocation;
	}
	
	public void getLocation(Context context,Handler handler){
		this.context=context;
		this.handler=handler;
		//初始化client
		locationClient = new AMapLocationClient(context);
		AMapLocationClientOption mOption = new AMapLocationClientOption();
		mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
		mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
		mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
		mOption.setInterval(10000);//可选，设置定位间隔。默认为2秒
		mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
		mOption.setOnceLocation(false);//可选，设置是否单次定位。默认是false
		mOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
		AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
		mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
		mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
		//设置定位参数
		locationClient.setLocationOption(mOption);
		// 设置定位监听
		locationClient.setLocationListener(locationListener);
		// 启动定位
		locationClient.startLocation();
	}
	
	
	/**
	 * 定位监听
	 */
	AMapLocationListener locationListener = new AMapLocationListener() {
		public void onLocationChanged(AMapLocation loc) {
			if (null != loc) {
				String latitude=Shared.readString(context, "latitude");
				String longitude=Shared.readString(context, "longitude");
				if(TextUtils.isEmpty(latitude)){
					//解析定位结果
					final Message message=new Message();
					message.what=Constant.TWO;
					message.obj=loc.getLatitude()+"@"+loc.getLongitude();
					handler.sendMessage(message);
					Shared.writeString(context, "latitude", String.valueOf(loc.getLatitude()));
					Shared.writeString(context, "longitude", String.valueOf(loc.getLongitude()));
				}else{
					LatLng l1=new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
					LatLng l2=new LatLng(loc.getLatitude(), loc.getLongitude());
					if(AMapUtils.calculateLineDistance(l1, l2)>=40){
						//解析定位结果
						final Message message=new Message();
						message.what=Constant.TWO;
						message.obj=loc.getLatitude()+"@"+loc.getLongitude();
						handler.sendMessage(message);
						Shared.writeString(context, "latitude", String.valueOf(loc.getLatitude()));
						Shared.writeString(context, "longitude", String.valueOf(loc.getLongitude()));
					}
				}
			} else {
				Toast.makeText(context, "定位失败", 1).show();
			}
		}
	};
	
	
	
}
